package com.walker.elk.netty.socket.client;


import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.DelimiterBasedFrameDecoder;
import io.netty.handler.codec.Delimiters;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.LengthFieldPrepender;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.util.CharsetUtil;

public class WalkerClientChannelInitializer extends ChannelInitializer<SocketChannel> {

	@Override
	protected void initChannel(SocketChannel channel) throws Exception {
		ChannelPipeline channelPipeline = channel.pipeline();  //获取管道对象
		
//		channelPipeline.addLast(new DelimiterBasedFrameDecoder(4096, Delimiters.lineDelimiter()));
		
		channelPipeline.addLast(new LengthFieldBasedFrameDecoder(Integer.MAX_VALUE, 0, 4, 0, 4));
		
		channelPipeline.addLast(new LengthFieldPrepender(4));
		
		channelPipeline.addLast(new StringDecoder(CharsetUtil.UTF_8));
		
		channelPipeline.addLast(new StringEncoder(CharsetUtil.UTF_8));
		//添加处理器（handler）
		channelPipeline.addLast(new WalkerNettyClientChannelHandler());
	}

}
